package com.pine.controller;

import com.pine.pojo.Resume;
import com.pine.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/add")
    public ModelAndView add() {
        System.out.println("resume..add()");
        return new ModelAndView("add");
    }

    @GetMapping("/upd")
    public ModelAndView upd(Long id) {
        System.out.println("resume..findById()");
        ModelAndView model = new ModelAndView();
        Resume resume = resumeService.findById(id);
        model.addObject("resume", resume);
        model.setViewName("upd");
        return model;
    }

    @GetMapping("/findAll")
    public ModelAndView findAll() {
        System.out.println("resume..findAll()");
        return toList();
    }

    @PostMapping("/insert")
    public ModelAndView insert(Resume resume) {
        System.out.println("resume..insert()");
       resumeService.insert(resume);

        return toList();
    }

    @PostMapping("/update")
    public ModelAndView update(Resume resume) {
        System.out.println("resume..update()");
        resumeService.update(resume);
        return toList();
    }

    @GetMapping("/delete")
    public ModelAndView delete(Long id) {
        System.out.println("resume..delete()");
        resumeService.delete(id);
        return toList();
    }

    private ModelAndView toList(){
        ModelAndView model = new ModelAndView();
        List<Resume> resumes = resumeService.findAll();
        model.addObject("resumes", resumes);
        model.setViewName("list");
        return model;
    }

}
